<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 找出数组中重复的元素 
    function duplicates(arr) {
      // 出现的次数大于1次可以归入到新的数组中
      let obj = {}
      let newArr = []
      arr.forEach(element => {
        let i = 0
        arr.forEach(ele => {
          if (ele === element) {
            i++
          }
        })
        // forEach不会遍历空数组
        obj[element] = i
      });
      for (let k in obj) {
        if (obj[k] > 1) {
          newArr.push(+k)
        }
      }
      return newArr
    }


    console.log(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]))
  </script>
</body>

</html>